using ACM.cmsarea08.DBManager;
using ACM.cmsarea08.mainconfiguration;
using MySql.Data.MySqlClient;
using System;
using System.Web;
using System.Data;

namespace ACM.cmsarea08.Administration.Core {
	public class DBClass : ACM.cmsarea08.DBManager.DBClass {
		ACM.cmsarea08.Administration.Core.DBHelper query = new DBHelper();
		private string _predefTable = ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle;
		public string TablePrefix {
			get { return _predefTable; }
		}
		
		public DataSet getAdminMenuItems(string lingua) {
			MySqlParameter[] myparam = new MySqlParameter[1];
			myparam[0] = new MySqlParameter("?lingua",MySqlDbType.VarChar);
			myparam[0].Value = (lingua.Trim()==""?mainconfiguration.MainConf.LinguaPredefinita:lingua);
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.getAdminMenuItems(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		public DataSet getAdminMenuItems() {
			return getAdminMenuItems(MainConf.LinguaPredefinita);
		}
		/*
		public DataSet getAdminMenu(string lingua) {
			MySqlParameter[] myparam = new MySqlParameter[1];
			myparam[0] = new MySqlParameter("?lingua",MySqlDbType.VarChar);
			myparam[0].Value = (lingua.Trim()==""?mainconfiguration.MainConf.LinguaPredefinita:lingua);
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminMenu(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		
		public DataSet getAdminMenu(string lingua,string roles) {
			MySqlParameter[] myparam = new MySqlParameter[1];
			myparam[0] = new MySqlParameter("?lingua",MySqlDbType.VarChar);
			myparam[0].Value = (lingua.Trim()==""?mainconfiguration.MainConf.LinguaPredefinita:lingua);
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminMenu(roles),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}	
		
		public DataSet GetCustomAdminMenu(string lingua,string[] roles) {
			string s = "";
			if(roles.Length==1) 
				s = query.GetCustomAdminMenu(roles[0],lingua,TablePrefix);
			else {
				for(int i = 0 ; i< roles.Length; i++) {
					s+= " (" + query.GetCustomAdminMenu(roles[i],lingua,TablePrefix) + ") UNION";
				}
				s = s.Substring(0,s.Length - 5);
			}
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, s);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}			
		
		public DataSet getAdminMenu() {
			return getAdminMenu(MainConf.LinguaPredefinita);
		}
		*/
		public DataSet GetDropAdminBlocchiVisivi() {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetDropAdminBlocchiVisivi());
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet GetDropAdminLanguage() {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetDropAdminLanguage());
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		public DataSet GetListMenu(string lingua) {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetListMenu(lingua));
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		public DataSet GetListMenu() {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetListMenu());
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		public DataSet GetMenuNames() {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetMenuNames());
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		public DataSet GetRoles(bool allRoles) {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetRoles(allRoles));
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		public DataSet GetRoles() {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetRoles(false));
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		public DataSet GetDropAdminComponents(string comptype ) {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, string.Format(query.GetDropAdminComponents(),
			                                                                  (comptype!=""?
			                                                                   ("and isMenu='1' "):
			                                                                   ""))
			                                                                  );
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}
		public DataSet GetDropAdminComponentsInstance() {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN,query.GetDropAdminComponentsInstance());
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}	
		public DataSet GetDropAdminComponentsInstance(string filtercomponentname) {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN,query.GetDropAdminComponentsInstance(filtercomponentname));
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;
		}			
		public DataSet GetDropAdminComponents() {
			return GetDropAdminComponents("");
		}		
		
		public DataSet getAdminContent(string lingua, ContentManagerTypes tipo,bool managed) {
//			MySqlParameter[] myparam = new MySqlParameter[2];
//			myparam[0] = new MySqlParameter("?lingua",MySqlDbType.VarChar);
//			myparam[1] = new MySqlParameter("?ctype",MySqlDbType.VarChar);
//			myparam[0].Value = (lingua.Trim()==""?mainconfiguration.MainConf.LinguaPredefinita:lingua);
//			switch (Convert.ToInt32(tipo) ) {
//				case 1:
//					myparam[1].Value="S";
//					break;
//				case 2:
//					myparam[1].Value = "C";
//					break;
//				default:
//					myparam[1].Value = "S";break;
//			}
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminSezioni(""),myparam);
//			//("select id,titolo from 
			//" +ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager 
			//where lingua=?lingua and tipo=?ctype  {0} order by titolo ;",filter);
			string joina = "";
			if(ACM.cmsarea08.mainconfiguration.MainConf.ApplyContentFilter && !ACM.cmsarea08.Users.CurrentUser.isRuolo("SysAdm") ) {
				joina+= "inner join " + TablePrefix + "contenutiruoli as ruoli " +
				 		"on ( INSTR('" + string.Join("~",ACM.cmsarea08.Users.CurrentUser.Ruoli) + "',ruoli.ruolo)>0 and " +
				 		" ( " ;
				
				if(Convert.ToInt16(tipo) == 2) {
					joina+= "(ruoli.tipo='C' and ruoli.id="+ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager.id or "+
						ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager.figliodi=ruoli.id)";
				} else
					joina+= "(ruoli.tipo='S' and ruoli.id="+ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager.id )";
				joina+="     )   )";
			}			
			
			
			DataSet ds = this.getMultilanguageDataSet(ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager",
			           ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager.id; " +
			           ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager.titolo ",
			           ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager.tipo='"+
			           (Convert.ToInt32(tipo)==2?"C":"S")+ "' " +(managed?"' and " + ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ 
			                                                      "  contenuti_manager.managed <>'-1'":""),
			           joina, //qui join ruoli
			           string.Join(",",ACM.cmsarea08.mainconfiguration.MainConf.LingueDisponibili),
			           ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+"contenuti_manager.titolo asc",
			           "id","tradotto_da","lingua",0,1000);
			
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			ds.Tables[0].TableName	 ="Table";
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet getAdminContent(string lingua, ContentManagerTypes tipo,string sectionfilter,bool managed) {
//			MySqlParameter[] myparam = new MySqlParameter[2];
//			myparam[0] = new MySqlParameter("?lingua",MySqlDbType.VarChar);
//			myparam[1] = new MySqlParameter("?ctype",MySqlDbType.VarChar);
//			
//			myparam[0].Value = (lingua.Trim()==""?mainconfiguration.MainConf.LinguaPredefinita:lingua);
//			switch (Convert.ToInt32(tipo) ) {
//				case 1:
//					myparam[1].Value="S";
//					break;
//				case 2:
//					myparam[1].Value = "C";
//					
//					break;
//				default:
//					myparam[1].Value = "S";break;
//			}
//			string filter = "";
//			if(sectionfilter.Trim() != ""){
//				filter = "and figliodi='"+sectionfilter+"'";
//			}
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminSezioni(filter),myparam);
			string joina = "";
			if(ACM.cmsarea08.mainconfiguration.MainConf.ApplyContentFilter && !ACM.cmsarea08.Users.CurrentUser.isRuolo("SysAdm") ) {
				joina+= "inner join " + TablePrefix + "contenutiruoli as ruoli " +
				 		"on ( INSTR('" + string.Join("~",ACM.cmsarea08.Users.CurrentUser.Ruoli) + "',ruoli.ruolo)>0 and " +
				 		" ( " ;
				
				if(Convert.ToInt16(tipo) == 2) {
					joina+= "(ruoli.tipo='C' and ruoli.id=cont.id or cont.figliodi=ruoli.id)";
				} else
					joina+= "(ruoli.tipo='S' and ruoli.id=cont.id )";
				joina+="     )   )";
			}			
			
			DataSet ds = this.ExecuteDataset(
				"select cont.id, concat(cont.titolo,' (',cont.lingua,')') as titolo " +
				"from " + ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager as cont " + joina +
				"where cont.tipo='"+(Convert.ToInt32(tipo)==2?"C":"S") + "' " +(managed?" and cont.managed <>'-1' ":"") +
			    (sectionfilter.Trim() != "" ? ("and cont.figliodi='"+sectionfilter+"'"):"") + 
			    " order by cont.titolo asc ");

			/*
			DataSet ds = this.getMultilanguageDataSet(ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager as cont",
			           "cont.id; cont.titolo ",
			           "cont.tipo='"+(Convert.ToInt32(tipo)==2?"C":"S") + "' " +(managed?" and cont.managed <>'-1' ":"") +
			           (sectionfilter.Trim() != "" ? ("and cont.figliodi='"+sectionfilter+"'"):""),
			           joina, //qui join ruoli
			           string.Join(",",ACM.cmsarea08.mainconfiguration.MainConf.LingueDisponibili),
			           "titolo asc",
			           "id","tradotto_da","lingua",0,1000);
			*/
			
			
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			ds.Tables[0].TableName	 ="Table";
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet getAdminContentSelected(string lingua,string sez, ContentManagerTypes tipo, bool managed) {
			//"lingua=?lingua and tipo=?ctype and figliodi=?selson order by titolo ;";
			try {
			string joina = "";
			if(ACM.cmsarea08.mainconfiguration.MainConf.ApplyContentFilter && !ACM.cmsarea08.Users.CurrentUser.isRuolo("SysAdm") ) {
				joina+= "inner join " + TablePrefix + "contenutiruoli as ruoli " +
				 		"on ( INSTR('" + string.Join("~",ACM.cmsarea08.Users.CurrentUser.Ruoli) + "',ruoli.ruolo)>0 and " +
				 		" ( " ;
				if(Convert.ToInt16(tipo) == 2) {
					joina+= "(ruoli.tipo='C' and ruoli.id=conma.id or conma.figliodi=ruoli.id)";
				} else
					joina+= "(ruoli.tipo='S' and ruoli.id=conma.id )";
				joina+="     )   )";
			}		
			DataSet ds = this.ExecuteDataset("select conma.id, concat(conma.titolo,' (',conma.lingua,')') as titolo " +
			                                 "from  " + ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager as conma " +
			                                 joina + 
			                                 " where conma.tipo='" + (Convert.ToInt16(tipo)==2?"C":"S") + "' and conma.figliodi='"+sez+"' " + 
											(managed==true?" and conma.managed<>'-1' ":"") + 
											"order by conma.lingua asc, conma.titolo asc ");
			/*
			DataSet ds = this.getMultilanguageDataSet(
							ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti_manager as conma",
							"conma.id; conma.titolo ",
							"conma.tipo='" + (Convert.ToInt16(tipo)==2?"C":"S") + "' and conma.figliodi='"+sez+"' " + 
							(managed==true?" and conma.managed<>'-1' ":""),
							joina,
							(lingua.Trim()==""?mainconfiguration.MainConf.LinguaPredefinita:lingua),
							"conma.titolo asc","id","tradotto_da","lingua",0,1000,false,true);
			*/							
//			HttpContext.Current.Response.Write("ESGUO: "  + sez);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
//			HttpContext.Current.Response.Write("cambio: "  + "table");
			ds.Tables[0].TableName	 ="Table";
			if(ds.Tables[0].Rows.Count==0)
				return null;
//			HttpContext.Current.Response.Write("cambio: "  + "righe");
			return ds;			
			} catch (Exception ex) {
//				HttpContext.Current.Response.Write("SONO ERRORE: "  + ex.ToString());
				return null;
			}
		}
		public DataSet getAdminArticleSelected(string lingua, string sez,string cat, bool managed) {
			//return "select id,titolo from " +ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti where  order by titolo ;";
			string joina = "";
			if(ACM.cmsarea08.mainconfiguration.MainConf.ApplyContentFilter && !ACM.cmsarea08.Users.CurrentUser.isRuolo("SysAdm") ) {
				joina+= "inner join " + TablePrefix + "contenutiruoli as ruoli " +
				 		"on ( INSTR('" + string.Join("~",ACM.cmsarea08.Users.CurrentUser.Ruoli) + "',ruoli.ruolo)>0 and " +
				 		" ( " +
								"(ruoli.tipo='A' and ruoli.id=conto.id) or " +
								"(ruoli.tipo='C' and ruoli.id=conto.cat_id or conto.sez_id=ruoli.id) or " +
								"(ruoli.tipo='S' and ruoli.id=conto.sez_id or conto.cat_id=ruoli.id) " +
					"     )   "+
						")"
						;
				
			}			
			DataSet ds = this.ExecuteDataset(
				"select conto.id, concat(conto.titolo,' (',conto.lingua,')') as titolo from " +
				ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti as conto " + 
				joina +
				" where sez_id='"+sez+"' and cat_id='"+cat+"' " + 
				(managed==true?" and managed<>'-1' ":"") + 
				" order by conto.lingua asc, conto.titolo asc ");
			
			
			
			/*
			DataSet ds = this.getMultilanguageDataSet(
							ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+ "contenuti as conto ",
							"conto.id; conto.titolo ",
							"sez_id='"+sez+"' and cat_id='"+cat+"' " + 
							(managed==true?" and managed<>'-1' ":""),
							joina,
							(lingua.Trim()==""?mainconfiguration.MainConf.LinguaPredefinita:lingua),
							"titolo asc","id","tradotto_da","lingua",0,1000,false,true);						
			*/
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			ds.Tables[0].TableName	 ="Table";
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}

		public DataSet GetAdminModuliInst(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminModuliInst(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet GetAdminComponentInst(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminComponentInst(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}		
		public DataSet GetAdminSezioniList(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminSezioniList(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet GetAdminCategorieList(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminCategorieList(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet GetAdminCategorieList(string id, int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[3];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[2] = new MySqlParameter("?id",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			myparam[2].Value = id;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminCategoria(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet GetAdminCategorieListEReV(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminCategorieListRev(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet GetAdminCategorieListEReV(string id, int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[3];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[2] = new MySqlParameter("?id",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			myparam[2].Value = id;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminCategoriaRev(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		
		public DataSet GetAdminBlocchiVisivi(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminBlocchiVisivi(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet GetAdminBloccoVisivo(string id) {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminBloccoVisivo(id));
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
#region Francesco
		//Francesco
		public int UpdateStandardConfigModule(string name, string desc, string viewsblock, string lang, int showmode, object formdata, string addons, string related, string itemid) {
			MySqlParameter[] myparam = new MySqlParameter[9];
			myparam[0] = new MySqlParameter("?mnome",MySqlDbType.VarChar);
			myparam[1] = new MySqlParameter("?mdesc",MySqlDbType.VarChar);
			myparam[2] = new MySqlParameter("?mlang",MySqlDbType.VarChar);
			myparam[3] = new MySqlParameter("?mbv",MySqlDbType.Int32);
			myparam[4] = new MySqlParameter("?mparam",MySqlDbType.Blob);
			myparam[5] = new MySqlParameter("?maddons",MySqlDbType.VarChar);
			myparam[6] = new MySqlParameter("?mid",MySqlDbType.Int32);
			myparam[7] = new MySqlParameter("?related",MySqlDbType.VarChar);			
			myparam[8] = new MySqlParameter("?showmode",MySqlDbType.VarChar);
			
			myparam[0].Value = name;
			myparam[1].Value = desc;
			myparam[2].Value = lang;
			if(viewsblock.Trim()!="")
				myparam[3].Value = viewsblock;
			else
				myparam[3].Value = "-1";
			myparam[4].Value = formdata;
			myparam[5].Value = addons;
			myparam[6].Value = itemid;
			myparam[7].Value = related;
			myparam[8].Value = showmode;
			
			int reso= MySqlHelper.ExecuteNonQuery(base.strCN, query.UpdataModuloConfig(),myparam);
			return reso;
			
		}
		public int InsertStandardConfigModule(string name, string desc, string viewsblock, string languages, int showmode, object formdata, string addons, string related, string idcomponente) {
			MySqlParameter[] myparam = new MySqlParameter[9];
			myparam[0] = new MySqlParameter("?mnome",MySqlDbType.VarChar);
			myparam[1] = new MySqlParameter("?mdesc",MySqlDbType.VarChar);
			myparam[2] = new MySqlParameter("?mbv",MySqlDbType.Int32);
			myparam[3] = new MySqlParameter("?mlang",MySqlDbType.VarChar);
			myparam[4] = new MySqlParameter("?mparam",MySqlDbType.Blob);
			myparam[5] = new MySqlParameter("?maddons",MySqlDbType.VarChar);
			myparam[6] = new MySqlParameter("?mid",MySqlDbType.Int32);
			myparam[7] = new MySqlParameter("?showmode",MySqlDbType.VarChar);
			myparam[8] = new MySqlParameter("?related",MySqlDbType.VarChar);	
			
			myparam[0].Value = name;
			myparam[1].Value = desc;
			if(viewsblock.Trim()!="")
				myparam[2].Value = viewsblock;
			else
				myparam[2].Value = "-1";
			myparam[3].Value = languages;
			myparam[4].Value = formdata;
			myparam[5].Value = addons;
			myparam[6].Value = idcomponente;
			myparam[7].Value = showmode.ToString();
			myparam[8].Value = related.ToString();
			
			
			int reso= MySqlHelper.ExecuteNonQuery(base.strCN, query.InsertModuloConfig(),myparam);
			return reso;
			
		}
		public bool CheckEsistenzaModulo(string nome) {
			return this.ExecuteRealScalar("select count(*) from "+ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+"moduli where titolo='" + nome + "' ;")>0?true:false;
		}
		
#endregion		

		public DataSet GetAdminTimeZones() {
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminTimezone());
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}

		public DataSet GetAdminTemplate(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminTemplate(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		//marco GetAdminLingue
		public DataSet GetAdminLingue(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminLingue(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		//marco GetAdminContenuti
		
		
		/*
		public DataSet GetAdminContenuti(int start, int offset) {
			MySqlParameter[] myparam = new MySqlParameter[2];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminContenuti(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		public DataSet GetAdminContenuti(string filtro, int start, int offset) {
			if(filtro==string.Empty)
				return GetAdminContenuti(start,offset);
			if(filtro=="-1")
				return GetAdminContenuti(start,offset);
			string[] filtri = new string[0];
			if(filtro.IndexOf('~')==-1)
				filtri = new string[1] { filtro };
			else {
				filtri = filtro.Split('~');
			}
			
			MySqlParameter[] myparam = new MySqlParameter[4];
			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
			myparam[0].Value = start;
			myparam[1].Value = offset;
			myparam[2] = new MySqlParameter("?filter1",MySqlDbType.VarChar);
			myparam[2].Value = (filtri.Length>=1?filtri[0]:string.Empty);
			myparam[3] = new MySqlParameter("?filter2",MySqlDbType.VarChar);
			myparam[3].Value = (filtri.Length>=2?filtri[1]:string.Empty);
			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminContenuti_filtered(),myparam);
			if (ds==null)
				return null;
			if(ds.Tables.Count==0)
				return null;
			if(ds.Tables[0].Rows.Count==0)
				return null;
			return ds;			
		}
		
		*/
//		public DataSet GetAdminContenutiEReV(int start, int offset) {
//			MySqlParameter[] myparam = new MySqlParameter[2];
//			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
//			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
//			myparam[0].Value = start;
//			myparam[1].Value = offset;
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminContenutiRev(),myparam);
//			if (ds==null)
//				return null;
//			if(ds.Tables.Count==0)
//				return null;
//			if(ds.Tables[0].Rows.Count==0)
//				return null;
//			return ds;			
//		}
//		public DataSet GetAdminContenutiEReV(string filtro, int start, int offset) {
//			if(filtro==string.Empty)
//				return GetAdminContenutiEReV(start,offset);
//			string[] filtri = new string[0];
//			if(filtro.IndexOf('~')==-1)
//				filtri = new string[1] { filtro };
//			else {
//				filtri = filtro.Split('~');
//			}
//			
//			MySqlParameter[] myparam = new MySqlParameter[4];
//			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
//			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
//			myparam[0].Value = start;
//			myparam[1].Value = offset;
//			myparam[2] = new MySqlParameter("?filter1",MySqlDbType.VarChar);
//			myparam[2].Value = (filtri.Length>=1?filtri[0]:string.Empty);
//			myparam[3] = new MySqlParameter("?filter2",MySqlDbType.VarChar);
//			myparam[3].Value = (filtri.Length>=2?filtri[1]:string.Empty);
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminContenutiRev_filtered(),myparam);
//			if (ds==null)
//				return null;
//			if(ds.Tables.Count==0)
//				return null;
//			if(ds.Tables[0].Rows.Count==0)
//				return null;
//			return ds;			
//		}
//		
//		
//		
//		public DataSet GetAdminSezioniEReV(int start, int offset) {
//			MySqlParameter[] myparam = new MySqlParameter[2];
//			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
//			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
//			myparam[0].Value = start;
//			myparam[1].Value = offset;
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetAdminSezioniReV(),myparam);
//			if (ds==null)
//				return null;
//			if(ds.Tables.Count==0)
//				return null;
//			if(ds.Tables[0].Rows.Count==0)
//				return null;
//			return ds;			
//		}
//		
//		public DataSet GetRevContenuti(int start, int offset, string idutente) {
//			MySqlParameter[] myparam = new MySqlParameter[2];
//			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
//			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
//			myparam[0].Value = start;
//			myparam[1].Value = offset;
//			string dove= "";
//			if(idutente.Trim()!="")
//				dove = "where contents.autore='"+idutente+"' "; 
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetRevContenuti(dove),myparam);
//			if (ds==null)
//				return null;
//			if(ds.Tables.Count==0)
//				return null;
//			if(ds.Tables[0].Rows.Count==0)
//				return null;
//			return ds;			
//		}
//		public DataSet GetRevSezioni(int start, int offset, string idutente) {
//			MySqlParameter[] myparam = new MySqlParameter[2];
//			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
//			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
//			myparam[0].Value = start;
//			myparam[1].Value = offset;
//			string dove= "where tipo ='S' ";
//			if(idutente.Trim()!="")
//				dove += "and contents.autore='"+idutente+"' "; 
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetRevSezioni(dove),myparam);
//			if (ds==null)
//				return null;
//			if(ds.Tables.Count==0)
//				return null;
//			if(ds.Tables[0].Rows.Count==0)
//				return null;
//			return ds;			
//		}
//		public DataSet GetRevCategorie(int start, int offset, string idutente) {
//			MySqlParameter[] myparam = new MySqlParameter[2];
//			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
//			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
//			myparam[0].Value = start;
//			myparam[1].Value = offset;
//			string dove= "where tipo ='C' ";
//			if(idutente.Trim()!="")
//				dove += "and contents.autore='"+idutente+"' "; 
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetRevCategorie(dove),myparam);
//			if (ds==null)
//				return null;
//			if(ds.Tables.Count==0)
//				return null;
//			if(ds.Tables[0].Rows.Count==0)
//				return null;
//			return ds;			
//		}
//		public DataSet GetRevContenuti(string filtro, int start, int offset, string idutente) {
//			if(filtro==string.Empty)
//				return GetRevContenuti(start,offset,idutente);
//			if(filtro=="-1")
//				return GetRevContenuti(start,offset,idutente);
//			string[] filtri = new string[0];
//			if(filtro.IndexOf('~')==-1)
//				filtri = new string[1] { filtro };
//			else {
//				filtri = filtro.Split('~');
//			}
//			
//			MySqlParameter[] myparam = new MySqlParameter[5];
//			myparam[0] = new MySqlParameter("?startl",MySqlDbType.Int32);
//			myparam[1] = new MySqlParameter("?offsetl",MySqlDbType.Int32);
//			myparam[0].Value = start;
//			myparam[1].Value = offset;
//			myparam[2] = new MySqlParameter("?filter1",MySqlDbType.VarChar);
//			myparam[2].Value = (filtri.Length>=1?filtri[0]:string.Empty);
//			myparam[3] = new MySqlParameter("?filter2",MySqlDbType.VarChar);
//			myparam[3].Value = (filtri.Length>=2?filtri[1]:string.Empty);
//			string dove = "";
//			if(idutente.Trim()!="")
//				dove = "and contents.autore='"+idutente+"' "; 
//			DataSet ds = MySqlHelper.ExecuteDataset(base.strCN, query.GetRevContenuti_filtered(dove),myparam);
//			if (ds==null)
//				return null;
//			if(ds.Tables.Count==0)
//				return null;
//			if(ds.Tables[0].Rows.Count==0)
//				return null;
//			return ds;			
//		}

		public int setAdminContenutiAbilita(string id) {
			return MySqlHelper.ExecuteNonQuery(base.strCN,query.setAdminContenutiAbilita(id));
		}
		
		public bool VerificaPredef(string DBActionTable){
			if (DBActionTable==string.Empty)
				return true;
			if (HStandard_SC(string.Format("show columns from {0} where field = 'predef';",
			                               ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle + DBActionTable)) == null)
				return false;
			else 
				return true;
		}
		public bool VerificaProtetto(string DBActionTable){
			if (DBActionTable==string.Empty)
				return true;
			if (HStandard_SC(string.Format("show columns from {0} where field = 'protected';",
			                               ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle + DBActionTable)) == null)
				return false;
			else 
				return true;
		}
		public bool IsPredef(string DBActionTable, string mainkey, string valore){
			if(VerificaPredef(DBActionTable)){
				int reso = ExecuteRealScalar(string.Format("select predef from {0}{1} where {2}='{3}' ;",mainconfiguration.MainConf.PrefissoTabelle,
				                                DBActionTable,mainkey,valore));
				if(reso==1)
					return true;
				return false;
			}
			else
				return false;
		}
		public bool IsProtected(string DBActionTable, string mainkey, string valore){
			if(VerificaProtetto(DBActionTable)){
				int reso = ExecuteRealScalar(string.Format("select protected from {0}{1} where {2}='{3}' ;",mainconfiguration.MainConf.PrefissoTabelle,
				                                DBActionTable,mainkey,valore));
				if(reso==1)
					return true;
				return false;
			}
			else
				return false;
		}
		
		/// <summary>
		/// Estende una non query
		/// </summary>
		/// <param name="sql"></param>
		/// <returns></returns>
		public int ExecuteScalar(string sql) {
			return ACM.cmsarea08.DBManager.mainConnection.Helper_NonQuery(sql);
		}
		/// <summary>
		/// Estende una non query
		/// </summary>
		/// <param name="sql"></param>
		/// <param name="error"></param>
		/// <returns></returns>
		public int ExecuteNonQuery_Logged(string sql,out string error) {
			error = "";
			int result = 0;
			try {
				result = ACM.cmsarea08.DBManager.mainConnection.Helper_NonQuery(sql);
				error="";
			} catch(Exception ex) {
				error = ex.ToString();
				result = 0;
			}
			return result;
		}	
		/// <summary>
		/// Estende una Scalar
		/// </summary>
		/// <param name="sql"></param>
		/// <param name="error"></param>
		/// <returns></returns>
		public object ExecuteScalar_Logged(string sql,out string error) {
			error = "";
			object _result = null;
			try {
				_result = ACM.cmsarea08.DBManager.mainConnection.Standard_SC(sql);
				error="";
			} catch(Exception ex) {
				error = ex.ToString();
				_result = null;
			}
			return _result;
		}			
		/// <summary>
		/// Estende una scalar e ritorna un int
		/// </summary>
		/// <param name="sql"></param>
		/// <returns>ritorna -1 se la query ritorna null </returns>
		public int ExecuteRealScalar(string sql) {
			object obj = ACM.cmsarea08.DBManager.mainConnection.Standard_SC(sql);
			try{	
				if(obj!=null)
					return Convert.ToInt32(ACM.cmsarea08.DBManager.mainConnection.Standard_SC(sql));
				else
					return -1;
			} catch {
				return -1;
			}
		}
			/// <summary>
		/// Esegue una query restituendo la prima riga,colonna del set di record richiesto. utilizza l'Helper di Connessione
		/// </summary>
		/// <param name="sql">Query SQL</param>
		/// <returns>Object. Necessita cast al tipo di dati richiesto in query</returns>		
		public object ExecuteScalarObj(string sql)
		{
			
			try{	
				return ACM.cmsarea08.DBManager.mainConnection.Standard_SC(sql);
				
			} catch {
				return null;
			}
		}
		public string getMenuItemParams(string id) {
			MySqlParameter[] myparam = new MySqlParameter[1];
			myparam[0] = new MySqlParameter("?tipomenuitem",MySqlDbType.Int32);
			myparam[0].Value = id;
			object o = MySqlHelper.ExecuteScalar(base.strCN, "select parametri from "+mainconfiguration.MainConf.PrefissoTabelle+"menu_tipolink where id=?tipomenuitem limit 1;",myparam);
			if(o==null) 
				return string.Empty;
			else {
				return o.ToString();
			}
			return string.Empty;
		}
		
		public DataSet ExecuteDataset(string sql) {
			return ACM.cmsarea08.DBManager.mainConnection.Helper_Dataset(sql);
		}
	}
	
	
	public enum ContentManagerTypes {
		Sezioni=1,
		Categorie=2
	}
	public enum VerificaEliminabile {
		Eliminabile,
		Protetto,
		Predefinito,
		ProtettoEPredefinito
		
	}
}

